package com.tom.demo.common.component;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@Component
public class FillDataHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //插入操作，自动填充
        log.info("公共字段自动填充insert");
        metaObject.setValue("createTime", new Date());
        metaObject.setValue("createBy", StpUtil.getLoginIdAsString());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新操作，自动填充
        log.info("公共字段自动填充update");
        metaObject.setValue("updateTime", new Date());
        metaObject.setValue("updateBy", StpUtil.getLoginIdAsString());
    }
}
